package com.fanhao.dllo.soundfm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.entity.AnchorInforBottomResult;
import com.fanhao.dllo.soundfm.tools.MyOnclickListener;
import com.fanhao.dllo.soundfm.utils.Circlelmage;
import com.fanhao.dllo.soundfm.utils.CountToOthers;
import com.fanhao.dllo.soundfm.volley.GetBitmap;

import java.util.List;

/**
 * Created by dllo on 16/1/25.
 */
public class AnchorInforAdapter extends RecyclerView.Adapter
        <AnchorInforAdapter.AnchorViewHolder> {
    private Context context;
    private List<AnchorInforBottomResult.ListEntity> list;
    private MyOnclickListener myOnclickListener;

    public void setMyOnclickListener(MyOnclickListener myOnclickListener) {
        this.myOnclickListener = myOnclickListener;
    }

    public AnchorInforAdapter(Context context) {
        this.context = context;
    }

    public void addBottomData(List<AnchorInforBottomResult.ListEntity> list) {
        this.list = list;
        Log.d("AnchorInforAdapter", "list.size():" + list.size());
        notifyDataSetChanged();
    }

    @Override
    public AnchorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.fragment_anchor_infor_adapter_item, parent, false);
        return new AnchorViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AnchorViewHolder holder, final int position) {
        if (list != null && list.size() > 0) {
            holder.titleTv.setText(list.get(position).getTitle());
            Log.d("AnchorInforAdapter", list.get(position).getTitle());
            holder.descripTv.setText("by " + list.get(position).getNickname());
            holder.playCountsTv.setText(
                    CountToOthers.getCountToWan(list.get(position).getPlaytimes()));
            holder.timeTv.setText(
                    CountToOthers.getCountToHour(list.get(position).getDuration()));
            holder.loveTv.setText(list.get(position).getLikes() + "");
            holder.commentsTv.setText(list.get(position).getComments() + "");
            GetBitmap.getBitmap(list.get(position).getCoverLarge(), holder.anchorInforAdapterIv);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list != null &&list.size()> 0) {
                        myOnclickListener.myListener(list.get(position).getTrackId(),0);
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ?
                list.size() : 0;
    }

    public class AnchorViewHolder extends RecyclerView.ViewHolder {
        private TextView titleTv, descripTv, playCountsTv, timeTv, loveTv, commentsTv;
        private Circlelmage anchorInforAdapterIv;

        public AnchorViewHolder(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.tvSubTitle_anchorInforAdapter);
            descripTv = (TextView) itemView.findViewById(R.id.tvDescrip_anchorInforAdapter);
            playCountsTv = (TextView) itemView.findViewById(R.id.tvPlayCounts_anchorInforAdapter);
            timeTv = (TextView) itemView.findViewById(R.id.tvTime_anchorInforAdapter);
            loveTv = (TextView) itemView.findViewById(R.id.tvLikes_anchorInforAdapter);
            commentsTv = (TextView) itemView.findViewById(R.id.tvComments_anchorInforAdapter);
            anchorInforAdapterIv = (Circlelmage) itemView.findViewById(R.id.iv_anchorInforAdapter);
        }
    }
}
